from django.views.generic import list_detail
from django.http import HttpResponse
from django.http import Http404
from django.http import HttpResponseRedirect
from profile.models import *
from django.shortcuts import get_object_or_404
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
#from friends import is_friends


def display(request, user_id):
    current_user = request.user
    addable = False

    if user_id == None:
        # Anonymous users needs to specify a userid to 
        # view any profile
        if not current_user.is_authenticated():
            return HttpResponseRedirect('%s?next=%s' % 
                    (settings.LOGIN_URL, request.path))
        user_id = request.user.get_profile().pk
        active_profile = True
    else:
        user = get_object_or_404(UserProfile, pk=user_id)
        active_profile = False
        addable = False
        if current_user.is_authenticated():
            current_user_pk = current_user.get_profile().pk
            active_profile = (user.pk == current_user_pk)
            addable = (not user.friends.filter(pk=current_user_pk).exists()) and current_user_pk != user.pk

    return list_detail.object_detail(
            request,
            queryset = UserProfile.objects.all(),
            template_name = "profile_detail.html",
            template_object_name = "current_user",
            object_id = user_id,
            extra_context = {
                'active_profile':active_profile,
                'addable':addable,
                }
            )

def bookshelf_list(request, user_id):
    current_user = request.user
    user = get_object_or_404(UserProfile, pk=user_id)
    active_profile = False
    addable = False
    if current_user.is_authenticated():
        current_user_pk = current_user.get_profile().pk
        active_profile = (user.pk == current_user_pk)

    return render_to_response( "bookshelf_list.html", 
            {   'current_user': user, 
                'active_profile':active_profile},
                context_instance=RequestContext(request))
